package com.ailiwu.www.ailiwu.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.afollestad.materialdialogs.MaterialDialog;
import com.ailiwu.www.ailiwu.Fragment.ClassifyFragment;
import com.ailiwu.www.ailiwu.Fragment.HomeFragment;
import com.ailiwu.www.ailiwu.Fragment.HotFragment;
import com.ailiwu.www.ailiwu.R;
import com.ailiwu.www.ailiwu.base.BaseActivity;
import com.ailiwu.www.ailiwu.utils.DataCleanManager;
import com.ailiwu.www.ailiwu.utils.SharedPrefUtils;

import java.util.LinkedList;


public class MainActivity extends BaseActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    /**
     * 侧边栏Drawer
     */
    private DrawerLayout drawer;

    private RadioButton rbHome;
    /**
     * 底部导航的Tabs
     */
    private RadioGroup rg_tabs;
    /**
     * 用于储存Fragment集合
     */
    private LinkedList<Fragment> fragmentList = new LinkedList<>();
    /**
     * 极光推送开关的item项索引
     */
    private int jPushCurPos = 0;

    private static int fragmentsNum = 3;
    private int mIndex;
    /**
     * 填充Viewpager的FlameLayout
     */
    private FrameLayout flContent;
    private Toolbar toolbar;
    private Button btn_search;
    private DrawerLayout drawerLayout;


    @Override
    protected void loadXml() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void getIntentData(Bundle savedInstanceState) {

    }

    @Override
    protected void initView() {
        //设置toolbar
         toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        getSupportActionBar().setDisplayShowTitleEnabled(false);


        /**设置侧边栏开关*/
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        /**找控件RadioGroup*/
        rg_tabs = (RadioGroup) findViewById(R.id.rg_tabs);
        rbHome = (RadioButton) findViewById(R.id.rb_home);
        flContent = (FrameLayout) findViewById(R.id.fl_content);
        btn_search = (Button) findViewById(R.id.btn_search);
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, SearchActivity.class));
            }
        });
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    }

//    private void setJPushNitification() {
//        CustomPushNotificationBuilder builder = new
//                CustomPushNotificationBuilder(MainActivity.this,
//                R.layout.customer_notitfication_layout,
//                R.id.icon,
//                R.id.title,
//                R.id.text);
//        // 指定定制的 Notification Layout
//        builder.statusBarDrawable = R.drawable.your_notification_icon;
//        // 指定最顶层状态栏小图标
//        builder.layoutIconDrawable = R.drawable.your_2_notification_icon;
//        // 指定下拉状态栏时显示的通知图标
//        JPushInterface.setPushNotificationBuilder(2, builder);
//
//
//    }

    @Override
    protected void initData() {
    }



    @Override
    protected void setListener() {
        rg_tabs.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.rb_home:
                        mIndex = 0;
                        break;
                    case R.id.rb_hot:
                        mIndex = 1;
                        break;
                    case R.id.rb_classify:
                        mIndex = 2;
                        break;
                    default:
                        break;
                }
                /**得到当前的Fragment对象*/
                Fragment fragment = (Fragment) adapter.instantiateItem(flContent, mIndex);
                /**默认选择第0个fragment*/
                adapter.setPrimaryItem(flContent, 0, fragment);
                /**提交事务*/
                adapter.finishUpdate(flContent);
            }
        });
        //默认显示的tab
        rg_tabs.check(0);
        rbHome.setChecked(true);
    }


    @Override
    public void onBackPressed() {
        drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_scanner://二维码
                //打开扫描界面扫描条形码或二维码
                Intent openCameraIntent = new Intent(MainActivity.this, CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
                return true;
            case android.R.id.home://navigation菜单
                return true;
            default:
                break;
        }

        return super.onOptionsItemSelected(item);
    }

    /**
     * NavigationView 的点击监听
     * @param item
     * @return
     */
    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        switch (id) {
            case R.id.set_identity:

                new MaterialDialog.Builder(MainActivity.this)
                    .title("请选择性别")
                    .items(R.array.gender)
                    .itemsCallbackSingleChoice(SharedPrefUtils.getInt(MainActivity.this, "GENDER", 1)==1?0:1
                            , new MaterialDialog.ListCallbackSingleChoice() {
                        @Override
                        public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                            SharedPrefUtils.saveInt(MainActivity.this,"GENDER",which+1);
                            new MaterialDialog.Builder(MainActivity.this)
                                    .title("请选择职位")
                                    .items(R.array.generation)
                                    .itemsCallbackSingleChoice(SharedPrefUtils.getInt(MainActivity.this,"GENERATION",0), new MaterialDialog.ListCallbackSingleChoice() {
                                        @Override
                                        public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                                SharedPrefUtils.saveInt(MainActivity.this,"GENERATION",which);
                                            return true; // allow selection
                                        }
                                    })
                                    .positiveText("确定")
                                    .show();
                            return true; // allow selection
                        }
                    })
                    .positiveText("确定")
                    .show();
                break;
            case R.id.set_JPush_openOrClose:
                String arry [] ={"开启","关闭"};
                new MaterialDialog.Builder(MainActivity.this)
                        .title("是否开启消息推送？")
                        .items(arry)
                        .itemsCallbackSingleChoice(SharedPrefUtils.getBoolean(MainActivity.this,"isJPush",true)==true?0:1, new MaterialDialog.ListCallbackSingleChoice() {
                            @Override
                            public boolean onSelection(MaterialDialog dialog, View view, int which, CharSequence text) {
                                SharedPrefUtils.saveBoolean(MainActivity.this,"isJPush",which==0?true:false);
                                return true; // allow selection
                            }
                        })
                        .positiveText("确定")
                        .show();

                break;
            case R.id.clear_SharedPreferences:
                //清空缓存
                DataCleanManager.cleanInternalCache(MainActivity.this);
                DataCleanManager.cleanExternalCache(MainActivity.this);
                Snackbar.make(toolbar,"已清空缓存",Snackbar.LENGTH_SHORT).show();
                break;
            case R.id.check_for_updates:
                Snackbar.make(toolbar, "当前已是最新版本,~你还更新做嘛嘞~", Snackbar.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    FragmentStatePagerAdapter adapter = new FragmentStatePagerAdapter(getSupportFragmentManager()) {
        @Override
        public Fragment getItem(int position) {
            Fragment fragment = null;
            switch (position) {
                case 0:
                    fragment = new HomeFragment();
                    break;
                case 1:
                    fragment = new HotFragment();
                    break;
                case 2:
                    fragment = new ClassifyFragment();
                    break;
            }
            return fragment;
        }

        @Override
        public int getCount() {
            return fragmentsNum;
        }
    };

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //处理扫描结果（在界面上显示）
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            String scanResult = bundle.getString("result");
            Intent intent = new Intent(MainActivity.this, ResultActivity.class);
            intent.putExtra("SCANRESULT", scanResult);
            startActivity(intent);
        }
    }

}
